package com.jjprojects.earutility;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * @author U678864
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FilesComparingTool
{
    JDialog dlgEARFileCompareTool = null;
    JTable tableFileCompare = null;
    
    Vector vecFirstFileContent = new Vector();
    Vector vecSecondFileContent = new Vector();
    
    public void compareFileContents(String strFirstFileName, String strSecondFileName)
    {
        vecFirstFileContent = extractAllLinesFromFile(strFirstFileName);
        vecSecondFileContent = extractAllLinesFromFile(strSecondFileName);
        System.out.println("First Vector->" + vecFirstFileContent);
        System.out.println("SecondVector->" + vecSecondFileContent);
        arrangeVectorsInCorrectOrder();
        System.out.println("First Vector->" + vecFirstFileContent);
        System.out.println("SecondVector->" + vecSecondFileContent);
        System.out.println("First Vector SIZE->" + vecFirstFileContent.size());
        System.out.println("SecondVector SIZE->" + vecSecondFileContent.size());
        
        //Displaying the Dialog
        showEARFilesCompareDialog();
    }
    
    public Vector extractAllLinesFromFile(String strFileName)
    {
        Vector vecFileContent = new Vector();
        String strCurrentRecord = null;
        
        try
        {
            BufferedReader bufrdrInputData = new BufferedReader(new FileReader(strFileName));
            while ((strCurrentRecord = bufrdrInputData.readLine()) != null)
            {
                vecFileContent.add(strCurrentRecord);
            }
        }
        catch(Exception e)
        {
            EARUtility.log4jEARUtility.fatal("Error in extracting file for file comparision", e);
        }
        
        return vecFileContent;        
    }
    public void arrangeVectorsInCorrectOrder()
    {
        int intFirstEarSize = vecFirstFileContent.size();
        int intSecondEarSize = vecSecondFileContent.size();
        
//        Vector vecMissedFilesBasicDetails = new Vector(1000);

        if(intFirstEarSize < intSecondEarSize)
        {
            //Take the Second ear file as Reference
            for(int i=0;i<intSecondEarSize;i++)
            {
                String strSecondEarContent = (String) vecSecondFileContent.get(i);

                int intPositionOfValueInFirstJar = checkSameLineAfterCurrentLine(vecFirstFileContent, strSecondEarContent, i);
                
                System.out.println("Jesus" + intPositionOfValueInFirstJar);
                
                if(intPositionOfValueInFirstJar < 0)
                {
                    continue;
                }
                
                if(intPositionOfValueInFirstJar >= 0)
                {
                    if(intPositionOfValueInFirstJar > i)
                    {
                        int intRowsNeedToInsert = intPositionOfValueInFirstJar - i;
                        for(int j=0;j<intRowsNeedToInsert;j++)
                        {
                            vecSecondFileContent.insertElementAt("", i);
                            i++;
                            intSecondEarSize++;
                        }
                    }
                }
                else
                {
                    vecFirstFileContent.insertElementAt("", i);
                }
            }
            
            intFirstEarSize = vecFirstFileContent.size();
            intSecondEarSize = vecSecondFileContent.size();
            
            int intSizeDifference = intSecondEarSize - intFirstEarSize;
            if(intSizeDifference > 0)
            {
                for(int i=0;i<intSizeDifference;i++)
                {
                    vecFirstFileContent.add("");
                }
            }
        }
        else if(intFirstEarSize > intSecondEarSize)
        {
            //Take the First ear file as Reference
            for(int i=0;i<intFirstEarSize;i++)
            {
                String strFirstEarContent = (String) vecFirstFileContent.get(i);
                int intPositionOfValueInSecondJar = checkSameLineAfterCurrentLine(vecSecondFileContent, strFirstEarContent, i);
                
                if(intPositionOfValueInSecondJar >= 0)
                {
                    if(intPositionOfValueInSecondJar >= 0)
                    {
                        if(intPositionOfValueInSecondJar > i)
                        {
                            int intRowsNeedToInsert = intPositionOfValueInSecondJar - i;
                            for(int j=0;j<intRowsNeedToInsert;j++)
                            {
                                vecFirstFileContent.insertElementAt("", i);
                                i++;
                                intFirstEarSize++;
                            }
                        }
                    }
                }
                else if(intPositionOfValueInSecondJar < 0)
                {
                    vecSecondFileContent.insertElementAt("", i);
                }
                else
                {
                    continue;
                }
            }
          
            intFirstEarSize = vecFirstFileContent.size();
            intSecondEarSize = vecSecondFileContent.size();
          
            int intSizeDifference = intFirstEarSize - intSecondEarSize;
            if(intSizeDifference > 0)
            {
                for(int i=0;i<intSizeDifference;i++)
                {
                    vecSecondFileContent.add("");
                }
            }
        }
        else if(intFirstEarSize == intSecondEarSize)
        {
            for(int i=0;i<intFirstEarSize;i++)
            {
                String strFirstEarContent = (String) vecFirstFileContent.get(i);
                String strSecondEarContent = (String) vecSecondFileContent.get(i);
                
                if(strFirstEarContent.equals(strSecondEarContent))
                {
                    continue;
                }
                if(strFirstEarContent.trim().length() > 0)
                {
                    int intPositionOfValueInSecondJar = checkSameLineAfterCurrentLine(vecSecondFileContent, strFirstEarContent, i);
                    
                    if(intPositionOfValueInSecondJar < 0)
                    {
                        vecSecondFileContent.insertElementAt("", i);
                        continue;
                    }
                }
                
                //Check in the First Jar
                if(strSecondEarContent.trim().length() > 0)
                {
                    int intPositionOfValueInFirstJar = checkSameLineAfterCurrentLine(vecFirstFileContent, strSecondEarContent, i);
                    
                    if(intPositionOfValueInFirstJar < 0)
                    {
                        vecFirstFileContent.insertElementAt("", i);
                    }
                }
//                System.out.println("I Value->" + i);
//                System.out.println("FirstFileContent  inside if->" + vecFirstFileContent);
//                System.out.println("SecondFileContent inside if->" + vecSecondFileContent);
            }
            intSecondEarSize = vecSecondFileContent.size();
            int intSizeDifference = intSecondEarSize - intFirstEarSize;
            int intFinalTotalSize = intFirstEarSize + intSizeDifference;
            if(intSizeDifference > 0)
            {
                for(int i=intFirstEarSize;i<intFinalTotalSize;i++)
                {
                    vecFirstFileContent.add("");
                }
            }
        }
//        int intReturnValue = showErrorMessageDialog(vecMissedFilesBasicDetails);
//        return intReturnValue;        
    }
    public int checkSameLineAfterCurrentLine(Vector vecToCheck, String strLineToCheck, int intIndex)
    {
        int intReturnValue = -1;
        
        int intSize = vecToCheck.size();
        String strCurrentData = null;
        for(int i=intIndex;i<intSize;i++)
        {
            strCurrentData = (String) vecToCheck.get(i);
            if(strCurrentData.startsWith(strLineToCheck))
            {
                intReturnValue = i;
                break;
            }
        }
        return intReturnValue;
    }
    
    public void showEARFilesCompareDialog()
    {
        int intTotalNumberOfRecords = vecSecondFileContent.size();
        dlgEARFileCompareTool = new JDialog(EARUtility.frmEARUtility, "EAR Compare details", false);

        JPanel panlFirstEARFile = new JPanel();
        JPanel panlSecondEARFile = new JPanel();
        panlFirstEARFile.setBackground(Color.WHITE);
        panlSecondEARFile.setBackground(Color.BLACK);

        CustomAbstractTableModel cstbAddFields = new CustomAbstractTableModel(
                this, intTotalNumberOfRecords, 3, true, 1);//rows,cols
        
        int intWidthForEachColumn = (EARUtility.intActualWidth - 20) / 2;
        
        TableColumn tabcolFirstEarContent = TableDesigner.createColumn(0, 450, "");
        TableColumn tabcolEqualOrNot = TableDesigner.createColumn(1, 20, "");
        TableColumn tabcolSecondEarContent = TableDesigner.createColumn(2, 450, "");

        tabcolFirstEarContent.setMinWidth(intWidthForEachColumn);
        tabcolEqualOrNot.setMinWidth(20);
        tabcolEqualOrNot.setMaxWidth(20);
        tabcolSecondEarContent.setMinWidth(intWidthForEachColumn);

        TableColumnModel tabcolmdlRecycleBinManager = TableDesigner
                .createColumnModelForThreeRow(tabcolFirstEarContent, tabcolEqualOrNot, tabcolSecondEarContent);
        JTableHeader tabheadRecycleBinManager = TableDesigner
                .createTableHeader(tabcolmdlRecycleBinManager);
        
        tableFileCompare = TableDesigner.createTable(cstbAddFields,
                tabcolmdlRecycleBinManager, tabheadRecycleBinManager, 0, 20,
                true, false, true, 0);
        tableFileCompare.setBackground(Color.WHITE);
        tableFileCompare.setForeground(Color.WHITE);
        
        CustomTableRenderer trpref = new CustomTableRenderer(this, 2);
        tableFileCompare.setDefaultRenderer(Object.class, trpref);

        tableFileCompare.setColumnSelectionAllowed(true);

        int intSize = vecFirstFileContent.size();
//        System.out.println("intSize = ->" + intSize);
        String strFirstFileContent = null;
        String strSecondFileContent = null;
        for(int i=0;i<intSize;i++)
        {
            strFirstFileContent = (String) vecFirstFileContent.get(i);
            strSecondFileContent = (String) vecSecondFileContent.get(i);
//            System.out.println(strFirstFileContent + "<->" + strSecondFileContent);
            
            tableFileCompare.setValueAt(strFirstFileContent,i,0);
            tableFileCompare.setValueAt(strSecondFileContent,i,2);
        }
        tableFileCompare.update(tableFileCompare.getGraphics());

        JScrollPane sclpanEARCompare = new JScrollPane(
                tableFileCompare, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        tableFileCompare.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {}
        });
        
        panlFirstEARFile.setLayout(new BorderLayout());
        panlFirstEARFile.add(sclpanEARCompare);

        dlgEARFileCompareTool.getContentPane().add(panlFirstEARFile);
//        dlgEARFileCompareTool.setSize(EARUtility.intActualWidth, EARUtility.intActualHeight);
        dlgEARFileCompareTool.setSize(900,700);
        dlgEARFileCompareTool.setVisible(true);
    }
    
    public static void main(String[] args)
    {
        new FilesComparingTool().compareFileContents("a.txt", "b.txt");
//        new FilesComparingTool().compareFileContents("b.txt", "a.txt");
    }
}
